<?php

namespace app\admin\controller;

use util\Ip;
use app\admin\model\AuthRule as Menu;

class Home extends Baseic
{

    /**
     * get admin/Home/index
     * 显示后台视图主框架
     * @return view
     */
    public function index()
    {
         // 清除tag:admin_cache_data标签的缓存数据
        \think\Cache::clear();

       // 取得菜单
       $db            = new Menu();
       $authRuleData  = $db->getAuthRuleData($this->userId,session('is_admin'));
       unset($db);
       $this->assign('uesrMenu',$authRuleData);
       return view();
    }

    /**
     * get admin/Home/welcome
     * 显示后台welcome视图
     * @return view
     */
    public function welcome()
    {
      if (session('last_login_time')) {
            //上次登录IP
            $last_login_ip = session('last_login_ip');
            $this->view->assign("last_login_ip", $last_login_ip);
            //上次登录地址
            $last_login_loc = Ip::find($last_login_ip);
            $this->view->assign("last_login_loc", implode(" ", $last_login_loc));
            //上次登录次数
            $this->view->assign("login_count", session('login_count'));
            //上次登录时间
            $this->view->assign("last_login_time", session('last_login_time'));

            unset($last_login_ip);
            unset($last_login_loc);
        }
        //本次登录IP
        $current_login_ip = $this->request->ip();
        $this->view->assign("current_login_ip", $current_login_ip);
        //本次登录地址
        $current_login_loc = Ip::find($current_login_ip);
        $this->view->assign("current_login_loc", implode(" ", $current_login_loc));
        //本次登录时间
        $this->view->assign("now_time",  time());

        unset($current_login_ip);
        unset($current_login_loc);

        return view();
    }

      /**
     * put admin/Home/welcome
     * 更新缓存
     * @return json
     */
    public function updateCache(){
      if ($this->request->isPut()) {
         // 清除tag:admin_cache_data标签的缓存数据
        $re   = \think\Cache::clear();
        if ($re) {
          $result = ['state'=>true,'msg'=>"缓存更新成功"];
        }else {
          $result = ['state'=>false,'msg'=>"服务器繁忙"];
        }
     }
      return json($result);
    }
}
